package com.hdq.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hdq.constant.MessageConstant;
import com.hdq.exception.DeleteException;
import com.hdq.mapper.NoticeMapper;
import com.hdq.result.PageResult;
import com.hdq.service.NoticeService;
import dto.NoticeAddDTO;
import dto.NoticeEditDTO;
import entity.Notice;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import vo.NoticePageVO;

import java.util.List;

@Service
@Slf4j
public class NoticeServiceImpl implements NoticeService {

    @Autowired
    private NoticeMapper noticeMapper;

    @Override
    public void insert(NoticeAddDTO noticeAddDTO) {
        Notice notice = new Notice();
        BeanUtils.copyProperties(noticeAddDTO,notice);
        noticeMapper.insert(notice);
    }


    @Override
    public PageResult selectByPage(Integer page, Integer pageSize) {
        PageHelper.startPage(page,pageSize);
        Page<NoticePageVO> list= noticeMapper.selectByPage(page,pageSize);

        return new PageResult(list.getTotal(),list.getResult());
    }

    @Override
    @Transactional
    public void delete(List<Long> ids) {
        try{
            noticeMapper.delete(ids);
        }catch (Exception e){
            log.error("删除异常：{}",e);
            throw  new DeleteException(MessageConstant.DELETE_FAIL);
        }
    }


    @Override
    public void update(NoticeEditDTO noticeEditDTO) {
        Notice notice = new Notice();
        BeanUtils.copyProperties(noticeEditDTO,notice);
        noticeMapper.update(notice);
    }
}
